import time
from selenium.webdriver.common.by import By
from common.Utils import BlogDriver

class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver = BlogDriver.driver #复制Blog类的成员driver给我当前类的成员
        self.driver.get(self.url)
#成功登录的测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan") #输入正确账号
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456") #输入正确密码
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #判断登录是否成功的标志就是看博客列表页的个人信息是否有昵称，如果下面查找时能找到说明登录成功
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        BlogDriver.getScreeShot()
        time.sleep(1)

#异常登录的测试用例1 —— 错误账号，错误密码；预期结果：显示“用户不存在”弹窗
    def LoginFailTest1(self):
        #先clear一下再重新输入
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("hello") #输入错误账号
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("111111") #输入错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        #判断是否登录失败
        alert = self.driver.switch_to.alert #获取窗口
        print(alert.text)
        assert alert.text == "用户不存在"
        alert.accept()

#异常登录的测试用例2 —— 正确账号，错误密码；预期结果：显示“密码错误”弹窗
    def LoginFailTest2(self):
        #先clear一下再重新输入
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan") #输入正确账号
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("111111") #输入错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        #判断是否登录失败
        alert = self.driver.switch_to.alert #获取窗口
        print(alert.text)
        assert alert.text == "密码错误"
        alert.accept()

#异常登录的测试用例3 —— 错误账号，正确密码；预期结果：显示“用户不存在”弹窗
    def LoginFailTest3(self):
        #先clear一下再重新输入
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("hello") #输入错误账号
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("111111") #输入错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert #获取窗口
        print(alert.text)
        assert alert.text == "用户不存在"
        alert.accept()

#异常登录的测试用例4 —— 无账号，无密码；预期结果：显示“账号或密码不能为空”弹窗
    def LoginFailTest4(self):
        #先clear一下再重新输入
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        #不输入账号密码
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert #获取窗口
        print(alert.text)
        assert alert.text == "账号或密码不能为空"
        alert.accept()

#异常登录的测试用例5 —— 正确账号，无密码；预期结果：显示“账号或密码不能为空”弹窗
    def LoginFailTest5(self):
        #先clear一下再重新输入
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan") #输入正确账号
        #不输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert #获取窗口
        print(alert.text)
        assert alert.text == "账号或密码不能为空"
        alert.accept()

#异常登录的测试用例6 —— 错误账号，无密码；预期结果：显示“账号或密码不能为空”弹窗
    def LoginFailTest6(self):
        #先clear一下再重新输入
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("hello") #输入错误账号
        #不输入密码
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert #获取窗口
        print(alert.text)
        assert alert.text == "账号或密码不能为空"
        alert.accept()

#异常登录的测试用例7 —— 无账号，正确密码；预期结果：显示“账号或密码不能为空”弹窗
    def LoginFailTest7(self):
        #先clear一下再重新输入
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        #不输入账号
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456") #输入正确密码
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert #获取窗口
        print(alert.text)
        assert alert.text == "账号或密码不能为空"
        alert.accept()

#异常登录的测试用例8 —— 无账号，错误密码；预期结果：显示“账号或密码不能为空”弹窗
    def LoginFailTest8(self):
        #先clear一下再重新输入
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        #不输入账号
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("111111") #输入错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert #获取窗口
        print(alert.text)
        assert alert.text == "账号或密码不能为空"
        alert.accept()

    def LoginFailTest(self):
        self.LoginFailTest1()
        self.LoginFailTest2()
        self.LoginFailTest3()
        self.LoginFailTest4()
        self.LoginFailTest5()
        self.LoginFailTest6()
        self.LoginFailTest7()
        self.LoginFailTest8()

